package com.collectplus.express.parcel;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.collectplus.express.BaseFragment;
import com.collectplus.express.R;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.ParcelDetailBeanNew;
import com.collectplus.express.model.ParcelSendBean;
import com.collectplus.express.tools.DensityUtils;

import java.util.ArrayList;

import droid.frame.umeng.UMeng;
import droid.frame.utils.lang.Str;

/***
 * 我的包裹-收到的的包裹列表
 *
 * @author JinFangXing <br>
 *         2016-9-24下午5:23:06
 */
public class BaseParcelFragment extends BaseFragment implements OnClickListener {

    protected ListView mListView;
    protected SwipeRefreshLayout mRefreshLayout;
    // 0-寄件，1-收件
    protected int type = 0;
    protected LinearLayout mEmptyLayout;
    /**
     * 记录当前Fragment是否在后台，避免造成处理接口数据混乱
     */
    protected boolean isCurrentRequest;
    /**
     * 记录点击的item
     */
    protected int clickPosition;
    protected ArrayList<ParcelSendBean> parcels;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getArguments().getInt("type");
        if (type == 0) {
            UMeng.click(getActivityContext(), "parcel_wait_pay_click");
        } else if (type == 1) {
            UMeng.click(getActivityContext(), "parcel_wait_send_click");
        } else if (type == 3) {
            UMeng.click(getActivityContext(), "parcel_send_click");
        } else {
            UMeng.click(getActivityContext(), "parcel_all_pay_click");
        }

    }

    @Override
    protected void findViewById() {
        super.findViewById();
        mListView = (ListView) findViewById(R.id.listview);
        mListView.setDividerHeight(DensityUtils.dip2px(getActivityContext(), 11));
        mListView.setDivider(null);
        mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ParcelSendBean parcel = parcels.get(position);
                clickPosition = position;
                showLoadingDialog("");
                isCurrentRequest = true;
                int isCover = 0;
                if (!Str.isEmpty(parcel.getOrderId2())) {
                    isCover = 1;
                }
                AppHttp.getInstance().getParcelDetailNew(parcel.getParcelId(), isCover);
            }
        });

        mRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        mEmptyLayout = (LinearLayout) findViewById(R.id.empty_receive_layout);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.parcel_detail:
                cancelLoadingDialog();
                try {
                    final AppResult<ParcelDetailBeanNew> result = JSON.parser(msg.obj, ParcelDetailBeanNew.class);// 解析可能出现问题
                    if (result.isSuccess()) {
                        ParcelDetailBeanNew parcelDetail = result.getResult();
                        // 避免混乱在后台接到的消息不多展示处理
                        if (parcelDetail != null && isCurrentRequest) {
                            Intent intent = new Intent(getActivityContext(), ParcelDetailActivity.class);
                            //包裹列表的信息
                            ParcelSendBean sendBean = parcels.get(clickPosition);
                            intent.putExtra("parcelStatus", sendBean.getParcelStatus());
                            intent.putExtra("orderStatus", sendBean.getOrderStatus());
                            intent.putExtra("parcelDetailBean", parcelDetail);
                            intent.putExtra("parcelId", sendBean.getParcelId());
                            startActivityForResult(intent, 101 + type);
                        } else {
                            // showToast("当前没有数据");
                        }
                    } else {
                        // 未知原因是否需要显示
                        showToast(result.getMessage());
                    }
                    break;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                isCurrentRequest = false;
                return false;
            default:
                return super.handleMessage(msg);
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.parcel_status_operate:
                String parcelId = (String) v.getTag();
                showLoadingDialog("");
                isCurrentRequest = true;
                AppHttp.getInstance().getPassportDetail(parcelId, 0);
                //                AppLogic.getInstance().showPassportDialog(getActivityContext(),parcelId,null,0);
                break;
            default:
                break;
        }
    }

    @Override
    public void onPause() {
        isCurrentRequest = false;
        super.onPause();
    }

}
